class AudioRecorder(private val context: Context) {
    private var mediaRecorder: MediaRecorder? = null
    private var isRecording = false
    private var outputFile: File? = null
    
    fun startRecording(): Boolean {
        if (isRecording) return false
        
        try {
            outputFile = createOutputFile()
            mediaRecorder = MediaRecorder().apply {
                setAudioSource(MediaRecorder.AudioSource.MIC)
                setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
                setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
                setOutputFile(outputFile?.absolutePath)
                setAudioSamplingRate(44100)
                setAudioEncodingBitRate(96000)
                prepare()
                start()
            }
            isRecording = true
            return true
        } catch (e: Exception) {
            e.printStackTrace()
            return false
        }
    }
    
    fun stopRecording(): File? {
        if (!isRecording) return null
        
        try {
            mediaRecorder?.apply {
                stop()
                release()
            }
            mediaRecorder = null
            isRecording = false
            return outputFile
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }
    }
    
    private fun createOutputFile(): File {
        val fileName = "recording_${System.currentTimeMillis()}.mp4"
        return File(context.getExternalFilesDir(null), fileName)
    }
} 